//
//  RecordsView.m
//  Pad-test
//
//  Created by mac on 2017/3/25.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "RecordsView.h"

@implementation RecordsView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.translatesAutoresizingMaskIntoConstraints = NO;
    [self resizeView:self.schedulingRecordsTV LineSpacingWithContent:self.schedulingRecordsTV.text];
    [self resizeView:self.outageRecordsTV LineSpacingWithContent:self.outageRecordsTV.text];
    [self resizeView:self.otherTV LineSpacingWithContent:self.otherTV.text];
    [self changeBgdViewHeight];
}

- (void)setModel:(SchedulingRecordsModel *)model{
    _model = model;
    self.recordOfDateL.text = model.recordOfDate;
    //self.dayImg.image = ;
    //self.showImgView.image = model.imageName.length > 0 ? [UIImage imageNamed:model.imageName] : nil;
    self.directorNameL.text = model.directorName;
    self.monitorNameL.text = model.monitorName;
    self.dispatcherNameL.text = model.dispatcherName;
    self.ledIndicatorTV.text = model.ledIndicator;
    [self resizeView:self.ledIndicatorTV LineSpacingWithContent:model.ledIndicator];
    self.schedulingRecordsTV.text = model.schedulingRecords;
    [self resizeView:self.schedulingRecordsTV LineSpacingWithContent:model.schedulingRecords];
    self.firePreventionL.text = model.firePrevention;
    self.workingConditionL.text = model.workingCondition;
    self.safetyProtectionL.text = model.safetyProtection;
    self.outageRecordsTV.text = model.outageRecords;
    [self resizeView:self.outageRecordsTV LineSpacingWithContent:model.schedulingRecords];
    self.otherTV.text = model.other;
    [self resizeView:self.otherTV LineSpacingWithContent:model.schedulingRecords];
    
    [self changeBgdViewHeight];
}


-(void)resizeView:(UITextView *)aView LineSpacingWithContent:(NSString *)content{
    
    //一、设置textView的行内间距
    aView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //二、设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 10;
    NSDictionary *attributes = @{
                                 NSFontAttributeName :[UIFont systemFontOfSize:16],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                };
    //获取当前文本的属性
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:content attributes:attributes];
    aView.attributedText = attrStr;
    
    //三、根据文本属性，获取高度
    NSRange range = NSMakeRange(0, attrStr.length);
    // 获取该段attributedString的属性字典
    NSDictionary *dic = [attrStr attributesAtIndex:0 effectiveRange:&range];
    // 计算文本的大小
    CGSize sizeToFit = [content boundingRectWithSize:CGSizeMake(516-20, MAXFLOAT) // 用于计算文本绘制时占据的矩形块
                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading // 文本绘制时的附加选项
                                        attributes:dic        // 文字的属性
                                           context:nil].size; // context上下文。包括一些信息，例如如何调整字间距以及缩放。该对象包含的信息将用于文本绘制。该参数可为nil
    NSLog(@"sizeToFit.height==%f",sizeToFit.height);
    if (aView == self.schedulingRecordsTV && sizeToFit.height+20>148) {
        self.recordsHeight.constant = sizeToFit.height+20;
    }
    if (aView == self.outageRecordsTV && sizeToFit.height+20>68) {
        self.outageHeight.constant = sizeToFit.height+20;
    }
    if (aView == self.otherTV && sizeToFit.height+20>68) {
        self.otherHeight.constant = sizeToFit.height+20;
    }
    
}
- (void)changeBgdViewHeight{
    
    //调整scrollview的contenSize
    self.bgdViewHeight.constant = 850 + (self.recordsHeight.constant-148) + (self.outageHeight.constant-68) + (self.otherHeight.constant-68);
}
@end
